
#include <jni.h>
#include <stdio.h>
#include <string.h>

#define dataSize 1920



JNIEXPORT jbyteArray JNICALL Java_yqwl_com_reborn_service_STC_1240_164_Henative_helloWorld
(JNIEnv* env, jobject obj, jstring jstr) {

	char* rtn = NULL;
	jclass clsstring = (*env)->FindClass(env, "java/lang/String");
	jstring strencode = (*env)->NewStringUTF(env, "GB2312");
	jmethodID mid = (*env)->GetMethodID(env, clsstring, "getBytes", "(Ljava/lang/String;)[B");
	jbyteArray barr = (jbyteArray)(*env)->CallObjectMethod(env, jstr, mid, strencode); // String .getByte("GB2312");
	jsize alen = (*env)->GetArrayLength(env, barr);
	jbyte* ba = (*env)->GetByteArrayElements(env, barr, JNI_FALSE);
	if (alen > 0) {
		rtn = (char*)malloc(alen + 1); //"\0"
		memcpy(rtn, ba, alen);
		rtn[alen] = 0;
	}
	(*env)->ReleaseByteArrayElements(env, barr, ba, 0);

	unsigned char* cstr = rtn;
	unsigned char dataBuffer[dataSize] = { 0 };//最终返回数据
	unsigned char buffer0[8][240] = { 0 };//一帧格式数据
	unsigned char bufferStr[5][288] = { 0 };//存储中文字模信息



	size_t str_len = strlen(cstr);
	printf("\n%d   <<==== length\n", str_len);
	for (size_t i = 0; i < str_len; i++)
	{
		printf("\n%d\n", cstr[i]);
	}



	size_t x_offset = (5 - str_len / 2) / 2;
	for (size_t i = 0; i < str_len / 2; i++)
	{
		unsigned char tchar[2] = { cstr[i * 2],cstr[(i * 2) + 1] };
		getHzKCode(tchar, bufferStr[i]);
	}

	//填充数据
	for (size_t i = 8; i < 56; i++)
	{
		for (size_t k = x_offset; k < 5; k++) //控制字符个数 
		{
			for (size_t l = 0; l < 6; l++)
			{
				dataBuffer[i * 30 + k * 6 + l] = bufferStr[k - x_offset][(i - 8) * 6 + l];// sb[k][i * 6 + k * 6 + l];
			}
		}
	}


	//转换数据
	for (size_t j = 0; j < 8; j++)
	{

		for (size_t i = 0; i < 240; i++)
		{
			unsigned char temp = 0x00;
			for (size_t k = 0; k < 8; k++)    //   第一轮  0—239 字节 范围 0-1919  //k系数用于控制行数
			{
				temp = temp | (dataBuffer[j * 240 + k * 30 + (i / 8)] >> (7 - (i % 8)) & 0x1) << (k); //此时通过k控制移位数
			}
			buffer0[j][i] = temp;
		}

	}

		for (int i = 7; i >= 0; i--) //无符号整数 ，不能最小就是0了，for循环递减不能瞎用
	{
		for (int j = 0; j < 240; j++)
		{
			dataBuffer[(-(i-7))*240+j]=buffer0[i][j];
		}
	
	}

	free(rtn);

	jbyteArray bytearray;
	bytearray = (*env)->NewByteArray(env, dataSize);
	(*env)->SetByteArrayRegion(env, bytearray, 0, dataSize, dataBuffer);
	return  bytearray;


}



/**********************************
* 得到汉字字符的字模信息，存入数组
* 参数：
*   *c:要得到字模信息的字符指针
*   buffer[]:存储字模信息的数组
* 无返回值
***********************************/
void getHzKCode(char* c, char buff[])
{
	unsigned char qh, wh;
	unsigned long offset;
	FILE* HZK;

	/*打开字库文件*/
#pragma warning(suppress : 4996)
	if ((HZK = fopen("HZK48S", "rb")) == NULL) {//c4996
		printf("Can't open haz16,Please add it?");
		//  getch();
		exit(0);
	}
	/*区码=内码(高字节)-160  位码=内码(低字节)-160*/
	qh = c[0] - 0xa0 - 15;//没注意居不是完整字库文件，坑死我了，难受啊马飞
	wh = c[1] - 0xa0;
	// qh = *(c)-0xa0;            /*10进制的160等于16进制的A0*/
	 //wh = *(c + 1) - 0xa0;          /*获得区码与位码*/
	offset = (94 * (qh - 1) + (wh - 1)) * 288;/*计算该汉字在字库中偏移量*/
	fseek(HZK, offset, SEEK_SET);     /*将文件指针移动到偏移量的位置*/
	fread(buff, 288, 1, HZK);  /*从偏移量的位置读取32个字节*/
	printf("qh:%d,wh:%d,offset:%ld\n\r", qh, wh, offset);
}





























